package com.person.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.person.model.entity.AgentInfo;
import com.person.service.AgentInfoService;
import com.person.service.ImportService;
import jakarta.annotation.Resource;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ImportServiceImpl implements ImportService {

    @Resource
    private AgentInfoService agentInfoService;

    @Override
    public void uploadFile(MultipartFile excelFile) {
        Workbook workbook;
        try {
            workbook = new XSSFWorkbook(excelFile.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Sheet sheet = workbook.getSheetAt(0);
        DataFormatter formatter = new DataFormatter();
        Iterator<Row> rowIterator = sheet.iterator();
        rowIterator.next(); // 如果第一行为标题，则跳过
        List<String> creditCodeList = agentInfoService.selectAllCreditCode();
        LambdaUpdateWrapper<AgentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        ArrayList<AgentInfo> agentInfos = new ArrayList<>();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            String excelCreditCode = formatter.formatCellValue(row.getCell(2));
            AgentInfo agentInfo = new AgentInfo();
            agentInfo.setName(formatter.formatCellValue(row.getCell(1)));
            agentInfo.setCreditCode(excelCreditCode);
            agentInfo.setContactPerson(formatter.formatCellValue(row.getCell(3)));
            agentInfo.setContactPhone(formatter.formatCellValue(row.getCell(4)));
            agentInfo.setAddress(formatter.formatCellValue(row.getCell(5)));
            agentInfo.setNotes(formatter.formatCellValue(row.getCell(6)));
            if (creditCodeList.contains(excelCreditCode)) {
                updateWrapper.clear();
                updateWrapper.eq(AgentInfo::getCreditCode, excelCreditCode)
                        .set(AgentInfo::getName, formatter.formatCellValue(row.getCell(1)))
                        .set(AgentInfo::getContactPerson, formatter.formatCellValue(row.getCell(3)))
                        .set(AgentInfo::getContactPhone, formatter.formatCellValue(row.getCell(4)))
                        .set(AgentInfo::getAddress, formatter.formatCellValue(row.getCell(5)))
                        .set(AgentInfo::getNotes, formatter.formatCellValue(row.getCell(6)));
                agentInfoService.update(updateWrapper);
            } else {
                agentInfos.add(agentInfo);
            }
        }
        agentInfoService.saveBatch(agentInfos);
        try {
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
